package com.zhz.company.biz;

import com.google.common.collect.Lists;
import com.zhz.common.enums.BizResponseEnum;
import com.zhz.common.tool.mybatisplus.base.core.contract.Page;
import com.zhz.common.tool.mybatisplus.base.core.contract.PageParam;
import com.zhz.common.utils.ConvertUtils;
import com.zhz.common.utils.enums.EnumUtils;
import com.zhz.company.contract.enums.AuditStateEnum;
import com.zhz.company.contract.enums.StateEnum;
import com.zhz.company.contract.request.CompanyDeleteRequest;
import com.zhz.company.contract.request.CompanyFindByIdRequest;
import com.zhz.company.contract.request.CompanySaveRequest;
import com.zhz.company.contract.request.CompanyUpdateRequest;
import com.zhz.company.contract.response.CompanyFindAllResponse;
import com.zhz.company.contract.response.CompanyFindByIdResponse;
import com.zhz.company.repository.service.CompanyService;
import com.zhz.model.company.contract.dto.CompanyDTO;
import com.zhz.model.company.contract.param.CompanyQueryParam;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zhouhengzhe
 * @date 2023/3/23
 */
@Component
public class CompanyBizService {

    @Autowired
    private CompanyService companyService;

    /**
     * 添加企业
     *
     * @param companySaveRequest 企业信息对象
     */
    public void add(CompanySaveRequest companySaveRequest) {
        CompanyDTO companyDTO = ConvertUtils.sourceToTarget(companySaveRequest, CompanyDTO.class);
        companyDTO.setAuditState(AuditStateEnum.WAIT_AUDIT.getCode())
                .setState(StateEnum.ACTIVATE.getCode());
        companyService.save(companyDTO);
    }


    /**
     * 更新企业
     *
     * @param companyUpdateRequest 企业信息对象
     */
    public void update(CompanyUpdateRequest companyUpdateRequest) {

        //1、根据id查询企业对象
        CompanyDTO companyDTO = companyService.getOne(CompanyQueryParam.builder().id(companyUpdateRequest.getId()).build());
        //判断错误
        BizResponseEnum.NOT_EXIST_COMPANY_INFO.assertFail(companyDTO);
        companyDTO.setName(companyUpdateRequest.getName())
                .setCompanyPhone(companyUpdateRequest.getCompanyPhone());
        companyService.update(companyDTO);
    }

    /**
     * g
     *
     * @param companyDeleteRequest 企业信息ID
     */
    public void deleteById(CompanyDeleteRequest companyDeleteRequest) {
        companyService.delete(companyDeleteRequest.getId());
    }


    /**
     * 根据id查询企业
     */
    public CompanyFindByIdResponse findById(CompanyFindByIdRequest companyFindByIdRequest) {
        CompanyDTO companyDTO = companyService.get(companyFindByIdRequest.getId());
        CompanyFindByIdResponse companyFindByIdResponse = ConvertUtils.sourceToTarget(companyDTO, CompanyFindByIdResponse.class);
        if (Objects.nonNull(companyFindByIdResponse)) {
            companyFindByIdResponse.setAuditStateDesc(EnumUtils.getEnumMessage(AuditStateEnum.class, companyFindByIdResponse.getAuditState()));
        }
        return companyFindByIdResponse;
    }

    /**
     * 查询企业列表
     */
    public CompanyFindAllResponse findAll() {
        List<CompanyDTO> companyDTOS = companyService.list(CompanyQueryParam.builder().build());
        List<CompanyFindByIdResponse> companyFindByIdResponses = ConvertUtils.sourceToTarget(companyDTOS, CompanyFindByIdResponse.class);
        if (CollectionUtils.isNotEmpty(companyFindByIdResponses)) {
            companyFindByIdResponses = companyFindByIdResponses.stream().peek(
                    i -> i.setAuditStateDesc(EnumUtils.getEnumMessage(AuditStateEnum.class, i.getAuditState()))
            ).collect(Collectors.toList());
        }
        return CompanyFindAllResponse.builder().findByIdResponse(companyFindByIdResponses).build();
    }

    /**
     * 分页查询
     *
     * @param companyQueryParam
     * @return
     */
    public Page<CompanyFindByIdResponse> page(CompanyQueryParam companyQueryParam) {
        PageParam<CompanyQueryParam> pageParam = new PageParam<>();
        pageParam.setPage(companyQueryParam.getPage())
                .setLimit(companyQueryParam.getLimit())
                .setParam(companyQueryParam);
        Page<CompanyDTO> companyDTOPage = companyService.page(pageParam);
        List<CompanyFindByIdResponse> companyFindByIdResponses = Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(companyDTOPage.getList())) {
            companyFindByIdResponses = companyDTOPage.getList().stream()
                    .map(i -> {
                        CompanyFindByIdResponse companyFindByIdResponse = ConvertUtils.sourceToTarget(i, CompanyFindByIdResponse.class);
                        companyFindByIdResponse.setAuditStateDesc(EnumUtils.getEnumMessage(AuditStateEnum.class, i.getAuditState()));
                        return companyFindByIdResponse;
                    })
                    .collect(Collectors.toList());
        }
        return Page.succeed(companyFindByIdResponses, companyDTOPage.getTotal());
    }
}
